﻿using DevExpress.XtraGrid;
using DevExpress.XtraPrinting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesProjectWinform.Helper
{
    public static class DataConverHelper
    {
        public static DataTable ToDataTable<T>(this List<T> data)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
            DataTable dt = new DataTable();
            for (int i = 0; i < properties.Count; i++)
            {
                PropertyDescriptor property = properties[i];
                dt.Columns.Add(property.Name, property.PropertyType);
            }
            object[] values = new object[properties.Count];
            foreach (T item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = properties[i].GetValue(item);
                }
                dt.Rows.Add(values);
            }
            return dt;
        }

        /// <summary>
        /// 将字典转换为 (ID)Name 格式的字符串
        /// </summary>
        /// <param name="dictionary">要转换的字典</param>
        /// <param name="separator">分隔符，默认为逗号</param>
        /// <returns>格式化的字符串</returns>
        public static string ToIdNameString(this Dictionary<string, string> dictionary, string separator = ",")
        {
            if (dictionary == null || dictionary.Count == 0)
                return string.Empty;

            var formattedItems = dictionary.Select(kvp => $"({kvp.Key}){kvp.Value}");
            return string.Join(separator, formattedItems);
        }

        /// <summary>
        /// 自动生成编码，格式为XX年月日XXXXX
        /// </summary>
        /// <param name="codeType">编码类型</param>
        /// <param name="lastCode">最后一个编码，用于递增，如果为空则从00001开始</param>
        /// <returns>生成的新编码</returns>
        public static string GenerateCode(CodeType codeType, string lastCode = null)
        {
            // 获取当前日期
            string dateStr = DateTime.Now.ToString("yyMMdd");
            
            // 根据类型获取前缀
            string prefix = GetPrefix(codeType);
            
            // 如果没有提供最后一个编码，或者最后一个编码格式不符合要求，则从00001开始
            int serialNumber = 1;
            
            // 尝试从最后一个编码中提取序列号
            if (!string.IsNullOrEmpty(lastCode) && lastCode.Length >= 13)
            {
                string lastDateAndSerial = lastCode.Substring(2); // 去掉前缀
                
                // 如果日期相同，则序列号加1；否则从00001开始
                if (lastDateAndSerial.StartsWith(dateStr))
                {
                    string lastSerialStr = lastDateAndSerial.Substring(6); // 获取序列号部分
                    if (int.TryParse(lastSerialStr, out int lastSerial))
                    {
                        serialNumber = lastSerial + 1;
                    }
                }
            }
            
            // 生成5位序列号，前导零填充
            string serialStr = serialNumber.ToString("00000");
            
            // 组合编码
            return $"{prefix}{dateStr}{serialStr}";
        }
        
        /// <summary>
        /// 获取不同类型的编码前缀
        /// </summary>
        /// <param name="codeType">编码类型</param>
        /// <returns>编码前缀</returns>
        public static string GetPrefix(CodeType codeType)
        {
            switch (codeType)
            {
                case CodeType.WareHouse:
                    return "WH"; // 仓库
                case CodeType.Location:
                    return "LC"; // 库位
                case CodeType.Area:
                    return "AR"; // 库区
                case CodeType.ExternalTag:
                    return "ET"; // 外部标签
                case CodeType.Shelves:
                    return "SH"; // 货架
                case CodeType.Processclass:
                    return "PC"; // 工艺分类
                case CodeType.ProcessManag:
                    return "PM"; // 工序管理
                case CodeType.Job:
                    return "J"; // 职位
                case CodeType.Dep:
                    return "D"; // 部门
                case CodeType.EMP:
                    return "E"; // 人员
                default:
                    return "XX"; // 默认
            }
        }
    }
    
    /// <summary>
    /// 编码类型枚举
    /// </summary>
    public enum CodeType
    {
        /// <summary>
        /// 仓库编码
        /// </summary>
        WareHouse,
        
        /// <summary>
        /// 库位编码
        /// </summary>
        Location,
        
        /// <summary>
        /// 库区编码
        /// </summary>
        Area,
        
        /// <summary>
        /// 货架编码
        /// </summary>
        Shelves,

        /// <summary>
        /// 工艺分类
        /// </summary>
        Processclass,
        /// <summary>
        /// 部门
        /// </summary>
        Dep,
        /// <summary>
        /// 职位
        /// </summary>
        Job,
        /// <summary>
        /// 人员
        /// </summary>
        EMP,
        /// <summary>
        /// 工序管理
        /// </summary>
        ProcessManag,
        /// <summary>
        /// 外部标签解析编码
        /// </summary>
        ExternalTag
    }
}
